package com.tkjt.pms.modules.common.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueComments;
import com.tkjt.pms.common.utils.TkGradeUtil;

import com.tkjt.pms.modules.common.mapper.TkIndexValueAdjustMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueCommentsMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.scoring.model.ScoreCommentSaveRequestModel;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridResponseModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class IndexBaseService {
    @Autowired
    private TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    private TkIndexValueCommentsMapper tkIndexValueCommentsMapper;
    @Autowired
    private TkIndexValueAdjustMapper tkIndexValueAdjustMapper;
    public void saveFinalRows(TkIndexValue tkIndexValue,String loginUser,String indexYearMonth) {
        String message = null;
        double oldGrade = 0.0;
        String month = indexYearMonth != null || !"".equals(indexYearMonth) ? indexYearMonth.substring(4) : "";
        TkGradeUtil.updateGrade(tkIndexValue,tkIndexValue.getGrade(),month);
        if (indexYearMonth != null && !"".equals(indexYearMonth)) {
            QueryWrapper<TkIndexValue> queryWrapperSdf = new QueryWrapper<>();
            queryWrapperSdf.lambda().eq(TkIndexValue::getId, tkIndexValue.getId());
            TkIndexValue oldTkIndexValue = tkIndexValueMapper.selectOne(queryWrapperSdf);
            switch (month) {
                case "01":
                    oldGrade = oldTkIndexValue.getM01();
                    break;
                case "02":
                    oldGrade = oldTkIndexValue.getM02();
                    break;
                case "03":
                    oldGrade = oldTkIndexValue.getM03();
                    break;
                case "04":
                    oldGrade = oldTkIndexValue.getM04();
                    break;
                case "05":
                    oldGrade = oldTkIndexValue.getM05();
                    break;
                case "06":
                    oldGrade = oldTkIndexValue.getM06();
                    break;
                case "07":
                    oldGrade = oldTkIndexValue.getM07();
                    break;
                case "08":
                    oldGrade = oldTkIndexValue.getM08();
                    break;
                case "09":
                    oldGrade = oldTkIndexValue.getM09();
                    break;
                case "10":
                    oldGrade = oldTkIndexValue.getM10();
                    break;
                case "11":
                    oldGrade = oldTkIndexValue.getM11();
                    break;
                case "12":
                    oldGrade = oldTkIndexValue.getM12();
                    break;
            }
            if(!oldTkIndexValue.getIndexExecutor().equals(loginUser)){
                //绩效管理员修改总经理评分，需要记录原总经理评分数据，被评分部门，指标名称，指标Code，评分年份，评分月份，原评分分数
                message = "绩效管理员修改总经理评分,指标Code:"+oldTkIndexValue.getIndexValueCode()+",被评分部门Code:"+oldTkIndexValue.getIndexDepart()+",评分年份:"+oldTkIndexValue.getYear()+",评分月份:"+month+",原评分分数："+oldGrade+",修改后评分:"+tkIndexValue.getGrade();
                //todo 绩效管理员修改总经理评分数据记录日志表中，目前没有，打印在日志中
                // systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
                log.info(message);
            }
            tkIndexValueMapper.updateById(tkIndexValue);
            message = "tk_index_value绩效数据保存成功";
            log.info(message);
            QueryWrapper<TkIndexValueComments> queryCommentsEntity = new QueryWrapper<>();
            queryCommentsEntity.lambda().eq(TkIndexValueComments::getIndexValueId, tkIndexValue.getId()).eq(TkIndexValueComments::getIndexValueM, month);
            tkIndexValueCommentsMapper.delete(queryCommentsEntity);
            int sunTotal=0;
            if(tkIndexValue.getScoreComments().size()!=0){
                List<ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean> scoreComments = tkIndexValue.getScoreComments();
                for (ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean valuesBean : scoreComments){
                    sunTotal++;
                TkIndexValueComments tkIndexValueComments = new TkIndexValueComments();
                tkIndexValueComments.setIndexValueId(tkIndexValue.getId());
                tkIndexValueComments.setIndexValueM(month);
                tkIndexValueComments.setIndexValueCommentSeq(1);
                //tkIndexValueComments.setIndexValueCommentTitle("总经理");
                //tkIndexValueComments.setIndexValueCommentTitle(tkIndexValue.);


                    tkIndexValueComments.setIndexValueCommentSeq(sunTotal);
                    tkIndexValueComments.setIndexValueCommentTitle(valuesBean.getTitle());
                    tkIndexValueComments.setIndexValueCommentCode(valuesBean.getCode());
                    tkIndexValueComments.setIndexValueComments(valuesBean.getContent());
                    tkIndexValueCommentsMapper.insert(tkIndexValueComments);
                    log.info("tk_index_value_comments评分意见成功添加:"+sunTotal);
                }

                //tkIndexValueComments.setIndexValueComments(tkIndexValue.getIndexValueComments());
                //tkIndexValueCommentsMapper.insert(tkIndexValueComments);
               // message = "tk_index_value_comments评分意见添加成功";
            }
            log.info(message);
        }
    }
}
